package triTree;

import java.util.Vector;

import transaction.Transaction;
import triBasic.*;


//For query
class ReadableTriNode implements INode {

	private NodeInfo node;
	
	//private Vector<NodeInfo> ancestors;
	private TriPath path;
	
	private Vector<NodeInfo> children;
	
	public ReadableTriNode(WritableTriNode node, Transaction tx) { 
		this.node = node.getNodeSnapshot().copy();
		int cnt = node.getAncestorCount();
		Vector<NodeInfo> ancestors = new Vector<NodeInfo>(cnt);
		
		for (int i = 0; i < cnt; ++i)
			ancestors.add(node.getNthAncestor(i));
		
		path = new TriPath(ancestors);
		
		cnt = node.getChildCount(tx);
		children = new Vector<NodeInfo> (cnt);
		for (int i = 0; i < cnt; ++i)
			children.add(node.getNthChild(i, tx).copy());
	}

	@Override
	public NodeInfo getNodeInfo() {
		return this.node;
	}

	@Override
	public TriPath getTriPath() {
		return this.path;
	}

	@Override
	public int getChildCount() {
		return this.children.size();
	}

	@Override
	public NodeInfo getNthChild(int ith) {
		return this.children.get(ith);
	}

	@Override
	public NodeInfo findChild(String tagName) {
		NodeInfo child;
		for (int i = 0; i < children.size(); ++i) {
			child = children.get(i);
			if (tagName.equals(child.getTagName()))
				return child;
		}
		
		return null;
	}

	@Override
	public boolean isNode(NodeInfo info) {
		return this.node.equals(info);
	}
	

}